<?php
namespace Api\Controller;
use Think\Controller;
class HotelController extends CommonController {
    public function addOrder(){
        $appid = "wx91169743893f29bf";
        $mch_id = "1482009672";
        $key = "BJLxqq12345678912345678920090823";

        if(empty($_POST['username']) && empty($_POST['tel'])){
            $arr = array("err_code"=>400,"err_msg"=>"请填写入住人相关信息！");
            $this->ajaxReturn($arr);
        }
        $map['id'] = $_POST['room_id'];
        $info = M("HotelRoom")->where($map)->find();
        if(!$info){
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
        $data['type'] = 'hotel';
        $data['wid'] = $info['wid'];
        $data['shop_id'] = $info['shop_id'];
        $data['goods_id'] = $info['id'];
        $data['openid'] = $_POST['openid'];
        $data['out_trade_no'] = $this->build_order_no();
        $data['title'] = $info['title'];
        $data['remark'] = "{$_POST['start_date']}至{$_POST['end_date']}";
        $data['content'] = "";
        $data['username'] = $_POST['username'];
        $data['tel'] = $_POST['tel'];
        $data['num'] = $_POST['days'];
        $data['pay_money'] = $info['price']*$_POST['days'];
        $data['status'] = 0;
        $data['is_pay'] = 0;
        $data['create_time'] = date("Y-m-d H:i:s");
        if(!M("Order")->add($data)){
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }

        $url = "https://".$_SERVER['HTTP_HOST'].U("Notify",array('type'=>'goods'));
        vendor('Weixinpay.WxPayJsApiPay');
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($data['remark']);
        $input->SetAttach($data['title']);
        $input->SetOut_trade_no($data['out_trade_no']);
        $input->SetTotal_fee(($data['pay_money']*100));
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        // $input->SetGoods_tag("test3");
        $input->SetNotify_url($url);   //支付回调地址，这里改成你自己的回调地址。
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($_POST['openid']);
        $input->SetAppid($appid);
        $input->SetMch_id($mch_id);

        try{
            $wxorder = \WxPayApi::unifiedOrder($input, $key);
        }catch(Exception $e){
            $arr = array("err_code"=>400,"err_msg"=>$e->getMessage());
            $this->ajaxReturn($arr);
        }
        if($wxorder['return_code'] == "FAIL"){
            $arr = array("err_code"=>400,"err_msg"=>$wxorder['return_msg']);
            $this->ajaxReturn($arr);
        }
        try{
            $jsApiParameters = $tools->GetJsApiParameters($wxorder, $key);
        }catch(Exception $e){
            $arr = array("err_code"=>400,"err_msg"=>$e->getMessage());
            $this->ajaxReturn($arr);
        }
        echo $jsApiParameters;
    }

    public function room(){
        $map['id'] = $_GET['id'];
        $info = M("HotelRoom")->where($map)->find();
        $info['pics'] = explode(",", $info['pics']);
        $this->ajaxReturn($info);
    }

    public function roomList(){
        $map = array();
        $map['status'] = 1;
        if($_GET['openid']){
            $map['openid'] = $_GET['openid'];
        }
        if($_GET['shop_id']){
            $map['shop_id'] = $_GET['shop_id'];
        }
        $list = M("HotelRoom")->where($map)->order("last_time desc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['pics'] = explode(',', $value['pics']);
        }
        $this->ajaxReturn($list);
    }

    public function roomAdd(){
        foreach ($_POST as $key => $value) {
            if(!is_numeric($value) && empty($value)){
                $arr = array("err_code"=>400,"err_msg"=>"请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        if($_POST['id']){
            $map['id'] = $_POST['id'];
            $map['openid'] = $_POST['openid'];
            if(M("HotelRoom")->where($map)->save($_POST)){
                $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"没有进行任何修改哦！");
                $this->ajaxReturn($arr);
            }
        }else{
            $eq['openid'] = $_POST['openid'];
            $_POST['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
            $_POST['wid'] = 1;
            $_POST['create_time'] = date("Y-m-d H:i:s");
            $_POST['status'] = 1;

            if(M("HotelRoom")->add($_POST)){
                $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
                $this->ajaxReturn($arr);
            }
        }
    }

    public function roomDel(){
        $map['id'] = $_GET['id'];
        $map['openid'] = $_GET['openid'];

        $new['stasus'] = 0;
        if(M("HotelRoom")->where($map)->save($new)){
            $arr = array("err_code"=>0, "err_msg"=>"删除成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }
    
}